package com.spring.util;

/**
 * @author liyuncong
 * @version 1.0
 * @file ClassUtils
 * @brief ClassUtils
 * @details ClassUtils
 * @date 2022-12-08
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-12-08               liyuncong          Created
 */
public abstract class ClassUtils {

    public static ClassLoader getDefaultClassLoader() {
        ClassLoader classLoader = null;
        try {
            classLoader = Thread.currentThread().getContextClassLoader();
        } catch (Throwable throwable) {
            // 无法访问线程上下文ClassLoader
        }
        if (classLoader == null) {
            classLoader = ClassUtils.class.getClassLoader();
            if (classLoader == null) {
                try {
                    classLoader = ClassLoader.getSystemClassLoader();
                } catch (Throwable throwable) {
                    // 无法访问系统ClassLoader
                }
            }
        }
        return classLoader;
    }

    public static Class<?> forName(String name, ClassLoader classLoader)
        throws ClassNotFoundException, LinkageError {

        // TODO class fro name
        return Class.forName(name);
    }

    public static boolean isPresent(String className, ClassLoader classLoader) {
        try {
            forName(className, classLoader);
            return true;
        } catch (IllegalAccessError error) {
            throw new IllegalStateException(
                "Readability mismatch in inheritance hierarchy of class [" +
                    className + "]" + error.getMessage(), error);
        } catch (Throwable ex) {
            // Typically, ex is ClassNotFoundException or NoClassDefFoundError...
            return false;
        }
    }
}
